package com.tallysystem.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tallysystem.dao.TallysDao;
import com.tallysystem.daoimpl.TallysDaoImpl;
import com.tallysystem.entity.Tallys;

@WebServlet("/showTallys")
public class GetTallysListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		TallysDao tallysDao = new TallysDaoImpl();
		//获得数据不分页
		List<Tallys> tallyslist = tallysDao.getAllTallys();
		request.getSession().setAttribute("tallyslist", tallyslist);
		//分页
		String page = request.getParameter("page");
		String row = request.getParameter("row");
		int npage = 0;
		int nrow = 0;
		int totalPage = 0;
		if (page == null) {
			npage = 1;
		} else {
			npage = Integer.parseInt(page);
			if (npage < 1)
				npage = 1;
		}

		if (row == null) {
			nrow = 12;
		} else {
			nrow = Integer.parseInt(row);
			int total = tallysDao.count();
			totalPage = (total + nrow - 1) / nrow;
			if (npage > totalPage)
				npage = totalPage;
		}
		// 计算下标
		int pos = (npage - 1) * nrow;
		List<Tallys> pagelists = tallysDao.queryPage(pos, nrow);
		// 当前页
		request.setAttribute("page", npage);
//		request.setAttribute("totalPage", totalPage);
		request.setAttribute("pagelists", pagelists);
		
		request.getRequestDispatcher("tallyslist.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);

	}

}
